//
//  LYYPublishViewController.m
//  Icar
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "LYYPublishViewController.h"
#import "LYYHeaderCollectionViewCell.h"
#import "LYYGuessLikeCollectionViewCell.h"
#import "LYYGuessSecondCollectionViewCell.h"
#import "LYYTopCollectionViewCell.h"
#import "BaseHeader.h"
#import "LYYWebviewViewController.h"
@interface LYYPublishViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSInteger rowCount;
}
//背景collectionview
@property (nonatomic,retain) UICollectionView *backCollectionView;
//标题栏
@property (nonatomic,retain) UICollectionView *headerCollectionView;
@property (nonatomic,retain) NSArray *array;//标题数组
//帖子
@property (nonatomic,retain) UICollectionView *allCollectionView;
@property (nonatomic,retain) UICollectionView *askCollectionView;
@property (nonatomic,retain) UICollectionView *jCollectionView;
//数据解析数组
@property (nonatomic,retain) NSMutableArray *allArray;
@property (nonatomic,retain) NSMutableArray *allTopArray;
@property (nonatomic,retain) NSMutableArray *jArray;
@property (nonatomic,retain) NSMutableArray *jTopArray;
@property (nonatomic,retain) NSMutableArray *askArray;
@property (nonatomic,retain) NSMutableArray *askTopArray;
//刷新&加载
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,assign)NSInteger allPage;
@property (nonatomic,assign)NSInteger jPage;
@property (nonatomic,assign)NSInteger askPage;
@end

@implementation LYYPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].nightModeColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"<%@",self.model.forumName]style:UIBarButtonItemStylePlain target:self action:@selector(backToLastVC)];
    [self setUpBackCollectionView];
    [self creatHeaderCollectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;//自适应滚动size
    self.array = @[@"全部贴",@"精品贴",@"提问帖"];
    self.allArray = [[NSMutableArray alloc]init];
    self.jArray = [[NSMutableArray alloc]init];
    self.askArray = [[NSMutableArray alloc]init];
    self.allTopArray = [[NSMutableArray alloc]init];
    self.jTopArray = [[NSMutableArray alloc]init];
    self.askTopArray = [[NSMutableArray alloc]init];
    [self setUpAllCollectionView];
    [self setUpAskCollectionView];
    [self setUpjCollectionView];
    [self headerRefresh];
    [self footerRefresh];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.allCollectionView reloadData];
    [self.allCollectionView setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].backColor];
    [self.jCollectionView reloadData];
    [self.jCollectionView setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].backColor];
    [self.askCollectionView reloadData];
    [self.askCollectionView setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].backColor];
    self.headerCollectionView.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
    [self.headerCollectionView reloadData];
    self.backCollectionView.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor ;
    [self.backCollectionView reloadData];
}

- (void)viewDidAppear:(BOOL)animated{
    [self.backCollectionView reloadData];
}


#pragma mark - back collectionview

- (void)setUpBackCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(VIEW_WIDTH, VIEW_HEIGHT);
    self.backCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,109,WIDTH(375), HEIGHT(667 - 64 - 45)) collectionViewLayout:flowLayout];
    flowLayout.minimumLineSpacing = 5;//最小行间距默认为10;
    flowLayout.minimumInteritemSpacing = 2.;

    self.backCollectionView.delegate = self;
    self.backCollectionView.dataSource = self;
    [self.view addSubview:self.backCollectionView];
    [self.backCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.backCollectionView.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor ;
    [self.backCollectionView setContentSize:CGSizeMake(VIEW_WIDTH * 3, 0)];
    self.backCollectionView.showsHorizontalScrollIndicator = NO;
    [self.backCollectionView reloadData];
}

#pragma mark - header colllectionView
- (void)creatHeaderCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(WIDTH(120), HEIGHT(40));
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.headerCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH(375), HEIGHT(45)) collectionViewLayout:flowLayout];
    self.headerCollectionView.showsHorizontalScrollIndicator = YES;
    self.headerCollectionView.delegate = self;
    self.headerCollectionView.dataSource = self;
    [self.headerCollectionView setContentSize:CGSizeMake(WIDTH(375), 0)];
    [self.view addSubview:self.headerCollectionView];
   
    [self.headerCollectionView registerClass:[LYYHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.headerCollectionView reloadData];
    
}

#pragma mark - 全部帖
- (void)setUpAllCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 2;
    self.allCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH(375), VIEW_HEIGHT) collectionViewLayout:flowLayout];
    self.allCollectionView.delegate = self;
    self.allCollectionView.dataSource = self;
    [self.backCollectionView addSubview:self.allCollectionView];
    [self.allCollectionView registerClass:[LYYTopCollectionViewCell class] forCellWithReuseIdentifier:@"topcell"];
    [self.allCollectionView registerClass:[LYYGuessLikeCollectionViewCell class] forCellWithReuseIdentifier:@"haveimagecell"];
    [self.allCollectionView registerClass:[LYYGuessSecondCollectionViewCell class] forCellWithReuseIdentifier:@"noimagecell"];
    self.allCollectionView.showsVerticalScrollIndicator = YES;
    
    [self.allCollectionView reloadData];
    
}

#pragma mark - 精华帖

- (void)setUpjCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 2;
    self.jCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(WIDTH(375),0, WIDTH(375), VIEW_HEIGHT) collectionViewLayout:flowLayout];
    self.jCollectionView.delegate = self;
    self.jCollectionView.dataSource = self;
    [self.jCollectionView registerClass:[LYYGuessLikeCollectionViewCell class] forCellWithReuseIdentifier:@"jcell"];
    [self.jCollectionView registerClass:[LYYGuessSecondCollectionViewCell class] forCellWithReuseIdentifier:@"jreuse"];
    [self.backCollectionView addSubview:self.jCollectionView];
    self.jCollectionView.showsVerticalScrollIndicator = YES;
    
    [self.jCollectionView reloadData];
}

#pragma mark - 提问帖

- (void)setUpAskCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 2;
    self.askCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(WIDTH(375 * 2), 0, WIDTH(375), VIEW_HEIGHT) collectionViewLayout:flowLayout];
    self.askCollectionView.delegate = self;
    self.askCollectionView.dataSource = self;
    [self.askCollectionView registerClass:[LYYGuessLikeCollectionViewCell class] forCellWithReuseIdentifier:@"acell"];
    [self.askCollectionView registerClass:[LYYGuessSecondCollectionViewCell class] forCellWithReuseIdentifier:@"areuse"];
    [self.backCollectionView addSubview:self.askCollectionView];

    self.askCollectionView.showsVerticalScrollIndicator = NO;
    
    [self.askCollectionView reloadData];
}

#pragma mark - 签订flowLayout 代理,设置item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.allCollectionView) {
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH(375), HEIGHT(50));
        }else{
            LYYAllModel *allModel = [[LYYAllModel alloc]init];
            allModel = [self.allArray objectAtIndex:indexPath.row];
            if (allModel.images.count == 0) {
                return CGSizeMake(WIDTH(375), HEIGHT(120));
            }else{
                return CGSizeMake(WIDTH(375), HEIGHT(190));
            }
        }
    }
    else if (collectionView == self.jCollectionView){
        LYYAllModel *jmodel = [[LYYAllModel alloc]init];
        jmodel = [self.jArray objectAtIndex:indexPath.row];
        if (jmodel.images.count == 0) {
            return CGSizeMake(WIDTH(375), HEIGHT(120));
        }else{
            return CGSizeMake(WIDTH(375), HEIGHT(190));
        }
    }
    else if (collectionView == self.askCollectionView){
        LYYAllModel *askmodel = [[LYYAllModel alloc]init];
        askmodel = [self.askArray objectAtIndex:indexPath.row];
        if (askmodel.images.count == 0) {
            return CGSizeMake(WIDTH(375), HEIGHT(120));
        }else{
            return CGSizeMake(WIDTH(375), HEIGHT(190));
        }
    }
    else if(collectionView == self.backCollectionView){
        return CGSizeMake(VIEW_WIDTH, HEIGHT(667 - 45 - 64));
    }else{
        return CGSizeMake(WIDTH(120), HEIGHT(40));
    }
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    if (self.allCollectionView == collectionView) {
        if (section == 0) {
            return UIEdgeInsetsMake(1, 1, 1, 1);
        }else{
            return UIEdgeInsetsMake(2, 2, 2, 2);
        }
    }
    else if (collectionView == self.jCollectionView || collectionView == self.askCollectionView){
        return UIEdgeInsetsMake(2, 2, 2, 2);
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (collectionView == self.allCollectionView) {
        return 2;
    }
    else{
        return 1;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.backCollectionView || collectionView == self.headerCollectionView) {
        return self.array.count;
    }
    else if (collectionView == self.allCollectionView){
        if (section == 0) {
            return self.allTopArray.count;
        }else{
            return self.allArray.count;
        }
    }
    else if (collectionView == self.askCollectionView){
            return self.askArray.count;
    }
    else if (collectionView == self.jCollectionView){
            return self.jArray.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.headerCollectionView) {
        LYYHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.titleLabel.text = self.array[indexPath.row];
        cell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
        cell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
        cell.selected = NO;
        for (NSInteger i = 0; i < self.array.count; i++) {
            if (indexPath.row == i) {
                cell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
                cell.lineView.hidden = YES;
            }
            if (indexPath.row == rowCount) {
                cell.titleLabel.textColor = BARCOLOR;
                cell.lineView.hidden = NO;
            }
        }
        return cell;
    }
    else if(collectionView == self.backCollectionView){
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        rowCount = indexPath.row;
        return cell;
    }
    else if (collectionView == self.allCollectionView){
        if (indexPath.section == 0) {
            LYYTopModel *tmodel = [self.allTopArray objectAtIndex:indexPath.row];
            LYYTopCollectionViewCell *topcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topcell" forIndexPath:indexPath];
            [topcell getTopModel:tmodel];
            topcell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
            topcell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            return topcell;
        }else{
            LYYAllModel *amodel = [self.allArray objectAtIndex:indexPath.row];
            if (amodel.images.count == 0) {
                LYYGuessSecondCollectionViewCell *noimagecell = [collectionView dequeueReusableCellWithReuseIdentifier:@"noimagecell" forIndexPath:indexPath];
                [noimagecell getNoImageHotModel:amodel];
                noimagecell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
                noimagecell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
                return noimagecell;
            }else{
                LYYGuessLikeCollectionViewCell *haveimagecell = [collectionView dequeueReusableCellWithReuseIdentifier:@"haveimagecell" forIndexPath:indexPath];
                [haveimagecell getHotModel:amodel];
                haveimagecell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
                haveimagecell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
                return haveimagecell;
            }
        }
    }
    else if (collectionView == self.jCollectionView){
        LYYAllModel *jmodel = [self.jArray objectAtIndex:indexPath.row];
        if (jmodel.images.count == 0) {
            LYYGuessSecondCollectionViewCell *jreuse = [collectionView dequeueReusableCellWithReuseIdentifier:@"jreuse" forIndexPath:indexPath];
            [jreuse getJModel:jmodel];
            jreuse.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
            jreuse.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            return jreuse;
             }
        else{
            LYYGuessLikeCollectionViewCell *jcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"jcell" forIndexPath:indexPath];
            [jcell getJModel:jmodel];
            jcell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
            jcell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            return jcell;
            }
        
    }
    else if (collectionView == self.askCollectionView){
        LYYAllModel *amodel = [self.askArray objectAtIndex:indexPath.row];
        if (amodel.images.count == 0) {
            LYYGuessSecondCollectionViewCell *areuse = [collectionView dequeueReusableCellWithReuseIdentifier:@"areuse" forIndexPath:indexPath];
            [areuse getAskModel:amodel];
            areuse.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
            areuse.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            return areuse;
        }else{
            LYYGuessLikeCollectionViewCell *acell = [collectionView dequeueReusableCellWithReuseIdentifier:@"acell" forIndexPath:indexPath];
            [acell getAskModel:amodel];
            acell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
            acell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            return acell;
        }
    }
    return nil;
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.headerCollectionView) {
        [self.backCollectionView setContentOffset:CGPointMake(VIEW_WIDTH * indexPath.row, 0) animated:YES];
    }
    else if (collectionView == self.allCollectionView){
        LYYWebviewViewController *webview = [[LYYWebviewViewController alloc]init];
        LYYAllModel *model = [self.allArray objectAtIndex:indexPath.row];
        webview.allModel = model;
        [self.navigationController pushViewController:webview animated:YES];
    }
    else if (collectionView == self.jCollectionView){
        
        LYYWebviewViewController *webview = [[LYYWebviewViewController alloc]init];
        LYYAllModel *model = [self.jArray objectAtIndex:indexPath.row];
        webview.allModel = model;
        [self.navigationController pushViewController:webview animated:YES];
        
    }
    else if (collectionView == self.askCollectionView){
        
        LYYWebviewViewController *webview = [[LYYWebviewViewController alloc]init];
        LYYAllModel *model = [self.askArray objectAtIndex:indexPath.row];
        webview.allModel = model;
        [self.navigationController pushViewController:webview animated:YES];
        
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == self.backCollectionView) {
        [self.headerCollectionView reloadData];
    }
    
}

#pragma mark - 返回上一页
- (void)backToLastVC{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 下拉刷新
- (void)headerRefresh{
    __block LYYPublishViewController *view = self;
    [self.allCollectionView addLegendHeaderWithRefreshingBlock:^{
        view.page = 1;
        [view getAllDataSource:1];
        _isUp = YES;
    }];
    
    [self.jCollectionView addLegendHeaderWithRefreshingBlock:^{
        view.page = 1;
        [view getjDataSource:1];
        _isUp = YES;
    }];

    [self.askCollectionView addLegendHeaderWithRefreshingBlock:^{
        view.page = 1;
        [view getAskDataSource:1];
        _isUp = YES;
    }];
    [self.askCollectionView.header beginRefreshing];
    [self.allCollectionView.header beginRefreshing];
    [self.jCollectionView.header beginRefreshing];
}
#pragma mark - 上拉加载
- (void)footerRefresh{
    
    __block LYYPublishViewController *view = self;
    [self.allCollectionView addLegendFooterWithRefreshingBlock:^{
        view.page = view.page + 1;
        [view getAllDataSource:view.page];
        _isUp = NO;
        
    }];
    
    [self.jCollectionView addLegendFooterWithRefreshingBlock:^{
        view.page = view.page + 1;
        [view getjDataSource:view.page];
        _isUp = NO;
    }];

    [self.askCollectionView addLegendFooterWithRefreshingBlock:^{
        view.page = view.page + 1;
        [view getAskDataSource:view.page];
        _isUp = NO;
    }];
}

#pragma mark - 数据解析
- (void)getAllDataSource:(NSInteger)number{
    NSString *url = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/bbs/newForumsv45/%ld?pageNo=%ld&pageSize=20&orderby=replyat&needImage=true&lastQuestionCreateAt=true&timestamp=463751905.777774", self.model.forumId,number];
    if (ISCONENNCTED == YES) {
        [AFNBlock getDataWithString:url block:^(id back) {
#pragma mark - 将数据写入本地沙盒
            NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *pathString = [pathArray lastObject];
            NSString *documentPath = [pathString stringByAppendingPathComponent:@"all.plist"];
            NSDictionary *dicpath = back;
            [dicpath writeToFile:documentPath atomically:YES];
            if (_isUp == YES) {
                [self.allArray removeAllObjects];
                [self.allTopArray removeAllObjects];
            }
            if (_isUp == NO){
                [self.allTopArray removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"topTopics"]) {
                LYYTopModel *model = [LYYTopModel modelWithDictionary:dic];
                [self.allTopArray addObject:model];
            }
            for (NSDictionary *dicc in [back objectForKey:@"topicList"]) {
                LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
                model.nickname = [[dicc objectForKey:@"author"] objectForKey:@"nickname"];
                model.userface = [[dicc objectForKey:@"author"] objectForKey:@"userface"];
                [self.allArray addObject:model];

            }
            [self.allCollectionView reloadData];
            [self.allCollectionView.header endRefreshing];
            [self.allCollectionView.footer endRefreshing];
        }];
    }
    if (ISCONENNCTED == NO) {
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *pathString = [pathArray lastObject];
        NSString *documentPath = [pathString stringByAppendingPathComponent:@"all.plist"];
        NSDictionary *fileDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
        for (NSDictionary *dic in [fileDic objectForKey:@"topTopics"]) {
            LYYTopModel *model = [LYYTopModel modelWithDictionary:dic];
            [self.allTopArray addObject:model];
        }
        for (NSDictionary *dicc in [fileDic objectForKey:@"topicList"]) {
            LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
            [self.allArray addObject:model];
        }
        [self.allCollectionView reloadData];
    }
}

#pragma mark - 精帖数据
- (void)getjDataSource:(NSInteger)number{
    
    NSString *url = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/bbs/newForumsv45/%ld?pageNo=%ld&pageSize=20&orderby=replyat&filter=pick&needImage=true&lastQuestionCreateAt=true&timestamp=463913254.395376",self.model.forumId,number];
    if (ISCONENNCTED == YES) {
        if (_isUp == YES) {
            [self.jArray removeAllObjects];
        }
        [AFNBlock getDataWithString:url block:^(id back) {
            NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *pathString = [pathArray lastObject];
            NSString *documentPath = [pathString stringByAppendingPathComponent:@"j.plist"];
            NSDictionary *dicpath = back;
            [dicpath writeToFile:documentPath atomically:YES];
            for (NSDictionary *dicc in [back objectForKey:@"topicList"]) {
                LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
                model.nickname = [[dicc objectForKey:@"author"] objectForKey:@"nickname"];
                model.userface = [[dicc objectForKey:@"author"]objectForKey:@"userface"];
                [self.jArray addObject:model];
                NSLog(@"jarray = %@",self.jArray);
            }
            [self.jCollectionView reloadData];
            [self.jCollectionView.header endRefreshing];
            [self.jCollectionView.footer endRefreshing];
            
        }];
    }
    if (ISCONENNCTED == NO) {
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *pathString = [pathArray lastObject];
        NSString *documentPath = [pathString stringByAppendingPathComponent:@"j.plist"];
        NSDictionary *fileDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
        for (NSDictionary *dicc in [fileDic objectForKey:@"topicList"]) {
            LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
            [self.jArray addObject:model];
        }
        [self.jCollectionView reloadData];
    }
    
}

#pragma mark - 提问帖数据解析

- (void)getAskDataSource:(NSInteger)number{
    NSString *url = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/bbs/newForumsv45/%ld?pageNo=%ld&pageSize=20&orderby=replyat&filter=question&needImage=true&lastQuestionCreateAt=true&timestamp=463911117.123921",self.model.forumId,number];
    if (ISCONENNCTED == YES) {
        if (_isUp == YES) {
            [self.askArray removeAllObjects];
            [self.askTopArray removeAllObjects];
        }
        [AFNBlock getDataWithString:url block:^(id back) {
            NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *pathString = [pathArray lastObject];
            NSString *documentPath = [pathString stringByAppendingPathComponent:@"ask.plist"];
            NSDictionary *dicpath = back;
            [dicpath writeToFile:documentPath atomically:YES];
            for (NSDictionary *dicc in [back objectForKey:@"topicList"]) {
                LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
                model.nickname = [[dicc objectForKey:@"author"]objectForKey:@"nickname"];
                model.userface = [[dicc objectForKey:@"author"]objectForKey:@"userface"];
                [self.askArray addObject:model];
            }
            [self.askCollectionView reloadData];
            [self.askCollectionView.header endRefreshing];
            [self.askCollectionView.footer endRefreshing];
            
        }];
    }
    if (ISCONENNCTED == NO) {
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *pathString = [pathArray lastObject];
        NSString *documentPath = [pathString stringByAppendingPathComponent:@"ask.plist"];
        NSDictionary *fileDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
        for (NSDictionary *dicc in [fileDic objectForKey:@"topicList"]) {
            LYYAllModel *model = [LYYAllModel modelWithDictionary:dicc];
            [self.askArray addObject:model];
        }
        [self.askCollectionView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
